home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / Mandlebrot / Mandlebrot Folder / DrawMandel.c < prev    next >
Text File  |  1990-05-09  |  5KB  |  222 lines

  1. /* *********************************************************************************
  2.      
  3.       FILE:            DrawMandel.c
  4.       
  5.      DESCRIPTION:    DrawMandel is the main loop.  Picks a color using the value 
  6.                      calculated by CalcMandel to index into the system CLUT.  
  7.                      Then uses the toolbox call RGBForeColor to set the rectangle
  8.                      color and finally a call to LineTo. This version doesn't require 
  9.                      a 68881.
  10.      AUTHOR:        Bruce E. Gladstone
  11.      
  12.      Copyright © 1990 by Bruce E. Gladstone, All Rights Reserved.
  13.      
  14.      Revision History:
  15.      ============================================================
  16.      5/1/90    - Release to Compuserve
  17.      ============================================================
  18.      
  19.      COMMENTS:
  20.      
  21.      None. 
  22.  
  23.    ******************************************************************************** */        
  24.  
  25. #include <stdio.h> 
  26. #include <MacTypes.h> 
  27. #include <WindowMgr.h>
  28. #include <MenuMgr.h>
  29. #include <EventMgr.h>
  30. #include <ToolboxUtil.h> 
  31. #include <ControlMgr.h>
  32. #include <color.h>
  33. #include <colortoolbox.h>
  34. #include "Mandelbrot.h"
  35.  
  36.  
  37. /* ---------------------------- Global Variables ---------------------------------- */
  38.  
  39. extern int                colorQD;
  40. extern int                quitFlag;
  41. extern int                linearFlag;
  42. extern int                custPict;
  43. extern int                numColorBits;
  44. extern int                numColors, brushSize;
  45. extern int                limit;
  46. extern int                n;
  47. extern long                startTime, now;
  48. extern float            res;
  49. extern float            centx, centy;
  50. extern float            xmin, xmax, ymin, ymax;
  51. extern float            delx, dely;
  52.  
  53. /* ---------------------------- Global MacTypes ----------------------------------- */
  54.  
  55. extern Str255            str;
  56. extern CTabHandle        myColorHandle;
  57. extern RGBColor            aColor;
  58. extern Rect                myRect, dragRect;
  59. extern WindowPtr        aboutWindow;        
  60. extern MenuHandle        appleMenu, mandelMenu, editMenu, xMenu, yMenu, resMenu, timeMenu;
  61. extern WindowPtr        myWindow;
  62.  
  63. /* ---------------------------  Local Prototypes  --------------------------------- */
  64.  
  65. void drawMandel ( void );
  66. int calcMandel ( float, float );
  67. int eventCheck ( void );
  68. void updateMenus ( void );
  69.  
  70.  
  71. /* --------------------------------------------------------------------------------
  72.     DrawMandel - 5/01/90 beg
  73.    -------------------------------------------------------------------------------- */
  74. void
  75. drawMandel()
  76. {
  77.     long    n, nx, ny;
  78.     long    xres, yres;
  79.     float    x, y, del;
  80.     Rect    aRect;    
  81.     int        eventResult;
  82.     
  83.     myRect = (*myWindow).portRect;
  84.     xres = myRect.right;
  85.     yres = myRect.bottom;
  86.     xmin = centx - res;
  87.     xmax = centx + res;
  88.     ymin = centy - res;
  89.     ymax = centy + res;
  90.     
  91.     delx = ( xmax - xmin )/( xres+1 );
  92.     dely = ( ymax - ymin )/( yres+1 );
  93.     if ( delx > dely )
  94.     {
  95.         dely = delx;
  96.     }
  97.     else 
  98.     {
  99.         delx = dely;
  100.     }
  101.     PenSize ( brushSize , brushSize );        
  102.     del = delx*brushSize;
  103.     y = ymin;
  104.     ny = 0;
  105.     nx = 0;
  106.     while ( ny < ( yres - 1 ))    
  107.     {
  108.         x  = xmin;
  109.         nx = 0;
  110.         while ( nx < ( xres - 1 ))
  111.         {
  112.             n = calcMandel ( x,y );
  113.             if ( colorQD && (numColorBits > 2 ))        /* color version */
  114.             {
  115.                 if ( linearFlag )
  116.                 {
  117.                     if (( n == 0 ) || ( n >= limit ))
  118.                     {
  119.                         n = numColors - 1;
  120.                     }
  121.                     else
  122.                     {
  123.                         n = n * numColors / limit;
  124.                     }
  125.                 }
  126.                 else
  127.                 {
  128.                     if (( n == 0 ) || ( n >= limit ))
  129.                     {
  130.                         n = numColors - 1;
  131.                     }
  132.                     else
  133.                     {
  134.                         n = n % numColors;
  135.                     }
  136.                 }
  137.                 aColor = (**myColorHandle).ctTable[n].rgb;
  138.                 RGBForeColor ( &aColor );
  139.                 LineTo ( nx, ny ); 
  140.             }
  141.             else                                /* black and white version */
  142.             {
  143.                 if (( n == 0 ) || ( n >= 256 ))
  144.                 {
  145.                     PenPat ( black );
  146.                 }
  147.                 else if ( n > 50 )
  148.                 {
  149.                     PenPat ( dkGray );
  150.                 }
  151.                 else if ( n > 10 )
  152.                 {
  153.                     PenPat ( gray );
  154.                 }
  155.                 else if ( n > 2 )
  156.                 {
  157.                     PenPat ( ltGray );
  158.                 }
  159.                 else
  160.                 {
  161.                     PenPat ( white );
  162.                 }
  163.                 LineTo ( nx+brushSize, ny );
  164.             }
  165.             if ( eventCheck () || quitFlag == TRUE ) goto breakOut;
  166.             nx = nx + brushSize;        /* end of nx loop */
  167.             x = x + del;
  168.         }
  169.         ny = ny + brushSize;            /* end of ny loop */
  170.         y = y + del;
  171.         MoveTo ( 0, ny );
  172.     }
  173.     breakOut:
  174.     {
  175.         EraseRect ( &myRect );
  176.         MoveTo ( 0,0 );
  177.         now = ( TickCount() - startTime )/60;
  178.         updateMenus();                /* Call update here so we can record time */
  179.     }
  180. }    /* drawMandel()  */
  181.  
  182. /* --------------------------------------------------------------------------------
  183.     calcMandel - 5/01/90 beg
  184.     calcMandel determines the color index for the line to be drawn
  185. ----------------------------------------------------------------------------------- */
  186.  
  187. int calcMandel ( creal, cimag )
  188.  
  189. float        creal, cimag;
  190. {
  191.     register int    count;
  192.     float            zr2, zi2;
  193.     float            zreal, zimag;
  194.     float            zreal1, zreal2;
  195.     
  196.     zreal = creal;
  197.     zimag = cimag;
  198.     zreal1 = 100.0;
  199.     zreal2 = 200.0;
  200.     for ( count = 1; count <= limit; count++ )
  201.     {
  202.         zr2   = zreal*zreal;
  203.         zi2   = zimag*zimag;
  204.         if (( zr2 + zi2 ) > 4.0 ) break;
  205.         zimag = 2*zreal*zimag + cimag;
  206.         zreal = zr2 - zi2 + creal;
  207.         if ( zreal == zreal2 ) 
  208.         {
  209.             count  = 0;
  210.             break;
  211.         }
  212.         zreal2 = zreal1;
  213.         zreal1 = zreal;
  214.     }
  215.     return ( count );
  216. }  /*  int calcMandel ( creal, cimag )  */
  217.  
  218. /* ===============================  EOF  ==========================================
  219.     Copyright © 1990 by Bruce E. Gladstone, All Rights Reserved.
  220.    ================================================================================ */
  221.  
  222.